package server

import (
	"app/pb"
	"context"
	"errors"
	"github.com/fwhezfwhez/errorx"
	"github.com/sirupsen/logrus"
	"google.golang.org/grpc"
	"net"
)

type student struct {
	pb.FindTeacherServer
}

func (s *student) SayTeacher(c context.Context, stu *pb.Student) (*pb.Teacher, error) {
	if stu.Name == "sa" && stu.Age == 23 {
		return &pb.Teacher{
			Name: "alex",
		}, nil
	} else {
		return nil, errors.New("don't have this student")
	}
}

func CreateService() {
	prefix, addr := "/grpc/server1", "127.0.0.1:8800"
	srv, err := NewServiceRegister(prefix, addr, 5)
	if err != nil {
		logrus.Fatal(errorx.Wrap(err))
	}

	go srv.ListenKeepChan()
	ser := grpc.NewServer()
	pb.RegisterFindTeacherServer(ser, new(student))
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		logrus.Fatal(errorx.Wrap(err))
	}

	go ser.Serve(listener)
	logrus.Info("grpc server start on port:8800")
}
